package com.goambu.driver.autenticacion.controller;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.StatusLine;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.location.Location;
import android.net.wifi.WifiConfiguration.Status;

import com.goambu.driver.GoAmbuDriverConstants;
import com.goambu.driver.autenticacion.entities.AlertaBean;
import com.goambu.driver.autenticacion.entities.EmergencyUnitBean;
import com.goambu.driver.autenticacion.entities.LocationBean;
import com.goambu.driver.autenticacion.entities.UsuarioBean;
import com.goambu.driver.autenticacion.model.GetEmergencyUnitResponse;
import com.goambu.driver.autenticacion.model.PutGeolocationResponse;
import com.goambu.driver.autenticacion.model.SetStatusResponse;
import com.goambu.driver.framework.controller.internet.HttpConnector;
import com.goambu.driver.framework.view.GPSTrackerService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class AmbulanciaController {
	public static AmbulanciaController instance = null;
	public LocationBean currentLocation = new LocationBean();
	public Location thisLocation = null;
	public float distanceToDestiny = 100000;

	// public int inAttention = 0;
	// public int driverTerminar = 0;

	public static AmbulanciaController getInstance() {
		if (instance == null) {
			instance = new AmbulanciaController();

		}
		return instance;
	}

	public float distanceToDestiny(AlertaBean approvedAttention) {
		if (thisLocation != null) {
			Location destinyLoc = new Location(thisLocation);
			destinyLoc.setLatitude(approvedAttention.location.coordinates
					.get(0));
			destinyLoc.setLongitude(approvedAttention.location.coordinates
					.get(1));
			float distance = thisLocation.distanceTo(destinyLoc);
			return distance;
		}
		return 10000000;
	}

	public String getDistanceToDestiny(AlertaBean approvedAttention) {
		if (thisLocation != null) {
			Location destinyLoc = new Location(thisLocation);
			destinyLoc.setLatitude(approvedAttention.location.coordinates
					.get(0));
			destinyLoc.setLongitude(approvedAttention.location.coordinates
					.get(1));
			float distance = thisLocation.distanceTo(destinyLoc);
			return distance + "";
		}
		return "";
	}

	public int llego() {
		AlertaBean approvedAttention = AutenticacionController.getInstance().currentUser.worker.emergencyUnit.approvedAtention;
		AlertaBean arrivedAttention = AutenticacionController.getInstance().currentUser.worker.emergencyUnit.arrivedAtention;

		if (arrivedAttention != null
		// && distanceToDestiny<=
		// GoAmbuDriverConstants.PARAM_LOCATION_RADIODISTLLEGADA
		) {
			return 2;
		}

		if (approvedAttention != null) {
			float distance = distanceToDestiny(approvedAttention);
			distanceToDestiny = distance;

			if (distanceToDestiny <= GoAmbuDriverConstants.PARAM_LOCATION_RADIODISTLLEGADA) {
				return 1;
			} else {
				return 0;
			}
		} else
			return 0;
	}

	public GetEmergencyUnitResponse GetEmergencyUnit(UsuarioBean currentUser) {
		String path = GoAmbuDriverConstants.SERVER_URL
				+ String.format(
						GoAmbuDriverConstants.AUTENTICACION_GETEMERGENCYUNITBYID,
						currentUser.worker.organizationId,
						currentUser.worker.workerid);

		Gson gs = new Gson();
		String strResponse;
		GetEmergencyUnitResponse objResponse = null;
		EmergencyUnitBean emergencyunit = currentUser.worker.emergencyUnit;

		HttpResponse respuesta = HttpConnector.makeGetRequest(path, "");

		if (respuesta != null) {
			try {
				strResponse = EntityUtils.toString(respuesta.getEntity());
				objResponse = gs.fromJson(strResponse,
						GetEmergencyUnitResponse.class);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				emergencyunit = null;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				emergencyunit = null;
			}
		}

		if (objResponse != null) {
			if (!objResponse.id.isEmpty()) {
				emergencyunit = new EmergencyUnitBean();
				emergencyunit.emergencyunitid = objResponse.id;
				emergencyunit.location = objResponse.location;
				emergencyunit.currentStatus = objResponse.currentStatus;
				emergencyunit.driverId = objResponse.driverId;
				emergencyunit.duration = objResponse.duration;
				emergencyunit.vehiclePlate = objResponse.vehiclePlate;
				emergencyunit.organizationId = objResponse.organizationId;
				currentUser.worker.emergencyUnit = emergencyunit;

				AmbulanciaController.getInstance()
						.GetEmergencyUnitApprovedAttention(currentUser);
				AmbulanciaController.getInstance()
						.GetEmergencyUnitArrivedAttention(currentUser);

			} else
				emergencyunit = null;
		} else
			emergencyunit = null;

		return objResponse;
	}

	public ArrayList<AlertaBean> GetEmergencyUnitAssignedAttention(
			UsuarioBean currentUser) {
		AmbulanciaController.getInstance().GetEmergencyUnit(currentUser);

		String path = GoAmbuDriverConstants.SERVER_URL
				+ String.format(
						GoAmbuDriverConstants.EMERGENCIA_GETEMERGENCYUNITASSIGNEDATTENTION,
						currentUser.worker.emergencyUnit.emergencyunitid,
						currentUser.worker.organizationId);

		Gson gs = new Gson();
		String strResponse;
		ArrayList<AlertaBean> objResponse = null;
		AlertaBean assignedalerta = currentUser.worker.emergencyUnit.assignedAtention;
		AlertaBean tmp = null;

		HttpResponse respuesta = HttpConnector.makeGetRequest(path, "");

		if (respuesta != null) {
			try {
				strResponse = EntityUtils.toString(respuesta.getEntity());
				Type listType = new TypeToken<ArrayList<AlertaBean>>() {
				}.getType();
				objResponse = gs.fromJson(strResponse, listType);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				assignedalerta = null;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				assignedalerta = null;
			}
		}

		if (objResponse != null) {
			if (objResponse.size() > 0) {
				for (int i = 0; i < objResponse.size(); i++) {
					tmp = objResponse.get(i);
					if (tmp.currentStatus.equals("ASSIGNED")) {
						assignedalerta = tmp;
						currentUser.worker.emergencyUnit.assignedAtention = assignedalerta;
						break;
					}
				}
			} else
				assignedalerta = null;
		} else
			assignedalerta = null;

		return objResponse;
	}

	public void GetEmergencyUnitApprovedAttention(UsuarioBean currentUser) {
		String path = GoAmbuDriverConstants.SERVER_URL
				+ String.format(
						GoAmbuDriverConstants.EMERGENCIA_GETEMERGENCYUNITASSIGNEDATTENTION,
						currentUser.worker.emergencyUnit.emergencyunitid,
						currentUser.worker.organizationId);

		Gson gs = new Gson();
		String strResponse;
		ArrayList<AlertaBean> objResponse = null;
		AlertaBean approvedalerta = currentUser.worker.emergencyUnit.approvedAtention;
		AlertaBean tmp = null;

		HttpResponse respuesta = HttpConnector.makeGetRequest(path, "");

		if (respuesta != null) {
			try {
				strResponse = EntityUtils.toString(respuesta.getEntity());
				Type listType = new TypeToken<ArrayList<AlertaBean>>() {
				}.getType();
				objResponse = gs.fromJson(strResponse, listType);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				approvedalerta = null;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				approvedalerta = null;
			}
		}

		if (objResponse != null) {
			if (objResponse.size() > 0) {
				for (int i = 0; i < objResponse.size(); i++) {
					tmp = objResponse.get(i);
					if (tmp.currentStatus.equals("CONFIRMED")) {
						approvedalerta = tmp;
						currentUser.worker.emergencyUnit.approvedAtention = approvedalerta;
						currentUser.worker.emergencyUnit.assignedAtention = null;
						currentUser.worker.emergencyUnit.arrivedAtention = null;
						break;
					}
				}
			} else {
				approvedalerta = null;
				currentUser.worker.emergencyUnit.approvedAtention = null;
				currentUser.worker.emergencyUnit.arrivedAtention = null;
				currentUser.worker.emergencyUnit.assignedAtention = null;
			}

		} else {
			approvedalerta = null;
			currentUser.worker.emergencyUnit.approvedAtention = null;
			currentUser.worker.emergencyUnit.arrivedAtention = null;
			currentUser.worker.emergencyUnit.assignedAtention = null;
		}

	}

	public void GetEmergencyUnitArrivedAttention(UsuarioBean currentUser) {
		String path = GoAmbuDriverConstants.SERVER_URL
				+ String.format(
						GoAmbuDriverConstants.EMERGENCIA_GETEMERGENCYUNITASSIGNEDATTENTION,
						currentUser.worker.emergencyUnit.emergencyunitid,
						currentUser.worker.organizationId);

		Gson gs = new Gson();
		String strResponse;
		ArrayList<AlertaBean> objResponse = null;
		AlertaBean arrivedalerta = currentUser.worker.emergencyUnit.approvedAtention;
		AlertaBean tmp = null;

		HttpResponse respuesta = HttpConnector.makeGetRequest(path, "");

		if (respuesta != null) {
			try {
				strResponse = EntityUtils.toString(respuesta.getEntity());
				Type listType = new TypeToken<ArrayList<AlertaBean>>() {
				}.getType();
				objResponse = gs.fromJson(strResponse, listType);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				arrivedalerta = null;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				arrivedalerta = null;
			}
		}

		if (objResponse != null) {
			if (objResponse.size() > 0) {
				for (int i = 0; i < objResponse.size(); i++) {
					tmp = objResponse.get(i);
					if (tmp.currentStatus.equals("ARRIVED")) {
						arrivedalerta = tmp;
						currentUser.worker.emergencyUnit.approvedAtention = null;
						currentUser.worker.emergencyUnit.arrivedAtention = arrivedalerta;
						currentUser.worker.emergencyUnit.assignedAtention = null;
						break;
					}
				}
			} else {
				arrivedalerta = null;

				currentUser.worker.emergencyUnit.arrivedAtention = null;
				currentUser.worker.emergencyUnit.assignedAtention = null;
			}

		} else {
			arrivedalerta = null;

			currentUser.worker.emergencyUnit.arrivedAtention = null;
			currentUser.worker.emergencyUnit.assignedAtention = null;
		}

	}

	public boolean EUApproveAttention() {
		UsuarioBean currentUser = AutenticacionController.getInstance().currentUser;

		if (HttpConnector.isNetworkAvailable()) {
			String path = "";

			Gson gs = new Gson();
			String strResponse;
			PutGeolocationResponse objResponse = null;

			if (currentUser == null) {
				return false;
			} else if (currentUser.worker == null) {
				return false;
			} else if (currentUser.worker.emergencyUnit == null) {
				return false;
			} else if (currentUser.worker.emergencyUnit.assignedAtention == null) {
				return false;
			} else if (currentUser.worker.emergencyUnit.assignedAtention.id
					.isEmpty()) {
				return false;
			} else if (!currentUser.worker.emergencyUnit.assignedAtention.id
					.isEmpty()
					&& !currentUser.worker.emergencyUnit.assignedAtention.organizationId
							.isEmpty()) {
				path = GoAmbuDriverConstants.SERVER_URL
						+ String.format(
								GoAmbuDriverConstants.EMERGENCIA_PUTEMERGENCYUNITASSIGNEDATTENTIONAPPROVE,
								currentUser.worker.emergencyUnit.emergencyunitid,
								currentUser.worker.emergencyUnit.assignedAtention.id,
								currentUser.worker.emergencyUnit.assignedAtention.organizationId);

				JSONObject parametros = new JSONObject();
				try {
					parametros
							.put("id",
									currentUser.worker.emergencyUnit.assignedAtention.id);
					parametros.put("emergencyUnitId",
							currentUser.worker.emergencyUnit.emergencyunitid);
					parametros
							.put("organizationId",
									currentUser.worker.emergencyUnit.assignedAtention.organizationId);
				} catch (JSONException e) {
					e.printStackTrace();
				}

				HttpResponse respuesta = HttpConnector.makePutRequest(path,
						parametros.toString());

				AutenticacionController.getInstance().getWorkerByID(
						currentUser.userid);
				if (respuesta.getStatusLine().getStatusCode() == GoAmbuDriverConstants.HTTPRESPONSE_STATUSCODE_NOCONTENT) {
					// AutenticacionController.getInstance().currentUser.worker.emergencyUnit.approvedAtention
					// =
					// AutenticacionController.getInstance().currentUser.worker.emergencyUnit.assignedAtention;
					// AutenticacionController.getInstance().currentUser.worker.emergencyUnit.assignedAtention
					// = null;
					return true;
				} else {
					return false;
				}
				// AutenticacionController.getInstance().getWorkerByID(currentUser.userid);
				// AmbulanciaController.getInstance().GetEmergencyUnitApprovedAttention(currentUser);
				// AmbulanciaController.getInstance().GetEmergencyUnitArrivedAttention(currentUser);

			}
			return false;
		}
		return false;
	}

	public boolean EUArriveForAttention() {
		UsuarioBean currentUser = AutenticacionController.getInstance().currentUser;

		if (HttpConnector.isNetworkAvailable()) {
			String path = "";

			Gson gs = new Gson();
			String strResponse;
			PutGeolocationResponse objResponse = null;

			if (currentUser == null) {
				return false;
			} else if (currentUser.worker == null) {
				return false;
			} else if (currentUser.worker.emergencyUnit == null) {
				return false;
			} else if (currentUser.worker.emergencyUnit.approvedAtention == null) {
				return false;
			} else if (currentUser.worker.emergencyUnit.approvedAtention.id
					.isEmpty()) {
				return false;
			} else if (!currentUser.worker.emergencyUnit.approvedAtention.id
					.isEmpty()
					&& !currentUser.worker.emergencyUnit.approvedAtention.organizationId
							.isEmpty()) {
				path = GoAmbuDriverConstants.SERVER_URL
						+ String.format(
								GoAmbuDriverConstants.EMERGENCIA_PUTEMERGENCYUNITASSIGNEDATTENTIONARRIVE,
								currentUser.worker.emergencyUnit.emergencyunitid,
								currentUser.worker.emergencyUnit.approvedAtention.id,
								currentUser.worker.emergencyUnit.approvedAtention.organizationId);

				JSONObject parametros = new JSONObject();
				try {
					parametros.put("id",
							currentUser.worker.emergencyUnit.approvedAtention.id);
					parametros.put("emergencyUnitId",
							currentUser.worker.emergencyUnit.emergencyunitid);
					parametros
							.put("organizationId",
									currentUser.worker.emergencyUnit.approvedAtention.organizationId);
				} catch (JSONException e) {
					e.printStackTrace();
				}

				HttpResponse respuesta = HttpConnector.makePutRequest(path,
						parametros.toString());

				if (respuesta.getStatusLine().getStatusCode() == GoAmbuDriverConstants.HTTPRESPONSE_STATUSCODE_NOCONTENT) {
					AutenticacionController.getInstance().currentUser.worker.emergencyUnit.arrivedAtention = AutenticacionController
							.getInstance().currentUser.worker.emergencyUnit.approvedAtention;
					AutenticacionController.getInstance().currentUser.worker.emergencyUnit.approvedAtention = null;
					return true;
				} else {
					return false;
				}
				// AutenticacionController.getInstance().getWorkerByID(currentUser.userid);

			}
			return false;
		}
		return false;

	}

	public boolean EUCloseAttention() {
		UsuarioBean currentUser = AutenticacionController.getInstance().currentUser;
		
		if (HttpConnector.isNetworkAvailable()) {
			String path = "";
			
			Gson gs = new Gson();
			String strResponse;
			PutGeolocationResponse objResponse = null;

			if (currentUser == null) {
				return false;
			} else if (currentUser.worker == null) {
				return false;
			} else if (currentUser.worker.emergencyUnit == null) {
				return false;
			} else if (currentUser.worker.emergencyUnit.arrivedAtention == null) {
				return false;
			} else if (currentUser.worker.emergencyUnit.arrivedAtention.id
					.isEmpty()) {
				return false;
			} else if (!currentUser.worker.emergencyUnit.arrivedAtention.id
					.isEmpty()
					&& !currentUser.worker.emergencyUnit.arrivedAtention.organizationId
							.isEmpty()) {
				path = GoAmbuDriverConstants.SERVER_URL
						+ String.format(
								GoAmbuDriverConstants.EMERGENCIA_PUTEMERGENCYUNITARRIVEDATTENTIONCLOSE,
								currentUser.worker.emergencyUnit.emergencyunitid,
								currentUser.worker.emergencyUnit.arrivedAtention.id,
								currentUser.worker.emergencyUnit.arrivedAtention.organizationId);

				JSONObject parametros = new JSONObject();
				try {
					parametros.put("id",
							currentUser.worker.emergencyUnit.arrivedAtention.id);
					parametros.put("emergencyUnitId",
							currentUser.worker.emergencyUnit.emergencyunitid);
					parametros
							.put("organizationId",
									currentUser.worker.emergencyUnit.arrivedAtention.organizationId);
				} catch (JSONException e) {
					e.printStackTrace();
				}

				HttpResponse respuesta = HttpConnector.makePutRequest(path,
						parametros.toString());

				AutenticacionController.getInstance().getWorkerByID(
						currentUser.userid);
				if (respuesta.getStatusLine().getStatusCode() == GoAmbuDriverConstants.HTTPRESPONSE_STATUSCODE_NOCONTENT) {
					// currentUser.worker.emergencyUnit.arrivedAtention = null;
					// currentUser.worker.emergencyUnit.approvedAtention = null;
					currentUser.worker.emergencyUnit.assignedAtention = null;
					return true;
				} else {
					return false;
				}
				// AutenticacionController.getInstance().getWorkerByID(currentUser.userid);
				// currentUser.worker.emergencyUnit.arrivedAtention = null;
				// currentUser.worker.emergencyUnit.approvedAtention = null;
				// currentUser.worker.emergencyUnit.assignedAtention = null;
				// return null;
			}
			return false;
		}
		return false;
		
	}

	public boolean EURejectAttention() {
		UsuarioBean currentUser = AutenticacionController.getInstance().currentUser;
		
		if (HttpConnector.isNetworkAvailable()) {
			String path = "";

			Gson gs = new Gson();
			String strResponse;
			PutGeolocationResponse objResponse = null;

			if (currentUser == null) {
				return false;
			} else if (currentUser.worker == null) {
				return false;
			} else if (currentUser.worker.emergencyUnit == null) {
				return false;
			} else if (currentUser.worker.emergencyUnit.assignedAtention == null) {
				return false;
			} else if (currentUser.worker.emergencyUnit.assignedAtention.id
					.isEmpty()) {
				return false;
			} else if (!currentUser.worker.emergencyUnit.assignedAtention.id
					.isEmpty()
					&& !currentUser.worker.emergencyUnit.assignedAtention.organizationId
							.isEmpty()) {
				path = GoAmbuDriverConstants.SERVER_URL
						+ String.format(
								GoAmbuDriverConstants.EMERGENCIA_PUTEMERGENCYUNITASSIGNEDATTENTIONAREJECT,
								currentUser.worker.emergencyUnit.emergencyunitid,
								currentUser.worker.emergencyUnit.assignedAtention.id,
								currentUser.worker.emergencyUnit.assignedAtention.organizationId);

				JSONObject parametros = new JSONObject();
				try {
					parametros.put("id",
							currentUser.worker.emergencyUnit.assignedAtention.id);
					parametros.put("emergencyUnitId",
							currentUser.worker.emergencyUnit.emergencyunitid);
					parametros
							.put("organizationId",
									currentUser.worker.emergencyUnit.assignedAtention.organizationId);
				} catch (JSONException e) {
					e.printStackTrace();
				}

				HttpResponse respuesta = HttpConnector.makePutRequest(path,
						parametros.toString());

				if (respuesta.getStatusLine().getStatusCode() == GoAmbuDriverConstants.HTTPRESPONSE_STATUSCODE_NOCONTENT) {
					currentUser.worker.emergencyUnit.approvedAtention = null;
					AutenticacionController.getInstance().getWorkerByID(
							currentUser.userid);
					return true;
				} else {
					return false;
				}

				// AutenticacionController.getInstance().getWorkerByID(currentUser.userid);
				/*
				 * AmbulanciaController.getInstance()
				 * .GetEmergencyUnitApprovedAttention(currentUser);
				 * AmbulanciaController.getInstance()
				 * .GetEmergencyUnitArrivedAttention(currentUser);
				 */

			}
			return false;
		}
		return false;

	}

	public boolean EUCancelAttention() {
		UsuarioBean currentUser = AutenticacionController.getInstance().currentUser;
		
		if (HttpConnector.isNetworkAvailable()) {
			String path = "";

			Gson gs = new Gson();
			String strResponse;
			PutGeolocationResponse objResponse = null;

			if (currentUser == null) {
				return false;
			} else if (currentUser.worker == null) {
				return false;
			} else if (currentUser.worker.emergencyUnit == null) {
				return false;
			} else if (currentUser.worker.emergencyUnit.approvedAtention == null) {
				return false;
			} else if (currentUser.worker.emergencyUnit.approvedAtention.id
					.isEmpty()) {
				return false;
			} else if (!currentUser.worker.emergencyUnit.approvedAtention.id
					.isEmpty()
					&& !currentUser.worker.emergencyUnit.approvedAtention.organizationId
							.isEmpty()) {
				path = GoAmbuDriverConstants.SERVER_URL
						+ String.format(
								GoAmbuDriverConstants.EMERGENCIA_PUTEMERGENCYUNITASSIGNEDATTENTIONACANCEL,
								currentUser.worker.emergencyUnit.emergencyunitid,
								currentUser.worker.emergencyUnit.approvedAtention.id,
								currentUser.worker.emergencyUnit.approvedAtention.organizationId);

				JSONObject parametros = new JSONObject();
				try {
					parametros.put("id",
							currentUser.worker.emergencyUnit.approvedAtention.id);
					parametros.put("emergencyUnitId",
							currentUser.worker.emergencyUnit.emergencyunitid);
					parametros
							.put("organizationId",
									currentUser.worker.emergencyUnit.approvedAtention.organizationId);
				} catch (JSONException e) {
					e.printStackTrace();
				}

				HttpResponse respuesta = HttpConnector.makePutRequest(path,
						parametros.toString());

				AutenticacionController.getInstance().getWorkerByID(
						currentUser.userid);
				if (respuesta.getStatusLine().getStatusCode() == GoAmbuDriverConstants.HTTPRESPONSE_STATUSCODE_NOCONTENT) {
					// AutenticacionController.getInstance().currentUser.worker.emergencyUnit.approvedAtention
					// = null;
					return true;
				} else {

					return false;
				}
				/*
				 * AmbulanciaController.getInstance().GetEmergencyUnitApprovedAttention
				 * (currentUser);
				 * AmbulanciaController.getInstance().GetEmergencyUnitArrivedAttention
				 * (currentUser);
				 */

			}
			return false;
		}
		return false;

	}

	public PutGeolocationResponse UpdateGeolocation() {
		UsuarioBean currentUser = AutenticacionController.getInstance().currentUser;

		if (HttpConnector.isNetworkAvailable()) {
			String path = "";

			Gson gs = new Gson();
			String strResponse;
			PutGeolocationResponse objResponse = null;

			double lat = currentLocation.coordinates.get(0);
			double lon = currentLocation.coordinates.get(1);

			if (lat != 0.00 && lon != 0.00) {
				if (currentUser == null) {
					return null;
				} else if (currentUser.worker == null) {
					return null;
				} else if (currentUser.worker.emergencyUnit == null) {
					return null;
				} else if (currentUser.worker.emergencyUnit.emergencyunitid
						.isEmpty()) {
					return null;
				} else if (!currentUser.worker.emergencyUnit.emergencyunitid
						.isEmpty()
						&& !currentUser.worker.emergencyUnit.organizationId
								.isEmpty()) {
					path = GoAmbuDriverConstants.SERVER_URL
							+ String.format(
									GoAmbuDriverConstants.EMERGENCIA_UPDATEGEOLOCATION,
									currentUser.worker.emergencyUnit.emergencyunitid,
									currentUser.worker.organizationId);

					JSONObject parametros = new JSONObject();
					JSONArray parametrosArr = new JSONArray();
					try {
						parametrosArr.put(lat);
						parametrosArr.put(lon);
						parametros.put("coordinates", parametrosArr);
					} catch (JSONException e) {
						e.printStackTrace();
					}

					HttpResponse respuesta = HttpConnector.makePutRequest(path,
							parametros.toString());

					return null;
				}
			}
			return null;
		}

		return null;
	}

	public boolean SetUnavailable() {
		UsuarioBean currentUser = AutenticacionController.getInstance().currentUser;

		if (HttpConnector.isNetworkAvailable()) {
			String path = "";

			Gson gs = new Gson();
			String strResponse;

			if (currentUser == null) {
				return false;
			} else if (currentUser.worker == null) {
				return false;
			} else if (currentUser.worker.emergencyUnit == null) {
				return false;
			} else if (currentUser.worker.emergencyUnit.emergencyunitid.isEmpty()) {
				return false;
			} else if (!currentUser.worker.emergencyUnit.emergencyunitid.isEmpty()
					&& !currentUser.worker.emergencyUnit.organizationId.isEmpty()) {
				path = GoAmbuDriverConstants.SERVER_URL
						+ String.format(
								GoAmbuDriverConstants.EMERGENCIA_SETEMERGENCYUNITUNAVAILABLE,
								currentUser.worker.emergencyUnit.emergencyunitid,
								currentUser.worker.organizationId);

				JSONObject parametros = new JSONObject();
				try {
					parametros.put("id",
							currentUser.worker.emergencyUnit.emergencyunitid);
					parametros.put("organizationId",
							currentUser.worker.organizationId);
				} catch (JSONException e) {
					e.printStackTrace();
				}

				HttpResponse respuesta = HttpConnector.makePutRequest(path,
						parametros.toString());

				AutenticacionController.getInstance().getWorkerByID(
						currentUser.userid);
				if (respuesta.getStatusLine().getStatusCode() == GoAmbuDriverConstants.HTTPRESPONSE_STATUSCODE_NOCONTENT) {
					return true;
				} else {
					return false;
				}

				/*
				 * if (respuesta != null) { try { strResponse =
				 * EntityUtils.toString(respuesta.getEntity()); objResponse =
				 * gs.fromJson(strResponse, SetStatusResponse.class); } catch
				 * (ParseException e) { e.printStackTrace();
				 * //currentUser.worker.emergencyUnit.currentStatus = "UNAVAILABLE";
				 * } catch (IOException e) { e.printStackTrace();
				 * //currentUser.worker.emergencyUnit.currentStatus = "UNAVAILABLE";
				 * } }
				 */
			}
			return false;
		}
		return false;
	}

	public boolean SetAvailable() {
		UsuarioBean currentUser = AutenticacionController.getInstance().currentUser;
		if (HttpConnector.isNetworkAvailable()) {
			String path = "";

			Gson gs = new Gson();
			String strResponse;

			if (currentUser == null) {
				return false;
			} else if (currentUser.worker == null) {
				return false;
			} else if (currentUser.worker.emergencyUnit == null) {
				return false;
			} else if (currentUser.worker.emergencyUnit.emergencyunitid.isEmpty()) {
				return false;
			} else if (!currentUser.worker.emergencyUnit.emergencyunitid.isEmpty()
					&& !currentUser.worker.emergencyUnit.organizationId.isEmpty()) {
				path = GoAmbuDriverConstants.SERVER_URL
						+ String.format(
								GoAmbuDriverConstants.EMERGENCIA_SETEMERGENCYUNITAVAILABLE,
								currentUser.worker.emergencyUnit.emergencyunitid,
								currentUser.worker.organizationId);

				JSONObject parametros = new JSONObject();
				try {
					parametros.put("id",
							currentUser.worker.emergencyUnit.emergencyunitid);
					parametros.put("organizationId",
							currentUser.worker.organizationId);
				} catch (JSONException e) {
					e.printStackTrace();
				}

				HttpResponse respuesta = HttpConnector.makePutRequest(path,
						parametros.toString());

				AutenticacionController.getInstance().getWorkerByID(
						currentUser.userid);
				if (respuesta.getStatusLine().getStatusCode() == GoAmbuDriverConstants.HTTPRESPONSE_STATUSCODE_NOCONTENT) {
					return true;
				} else {
					return false;
				}

			}
			return false;
		}
		return false;
	}

}
